= render 'heading'

- if @import.working?
  .progress.progress-striped.active
    #progress-text.progress-bar.progress-bar-green{ style: "width: #{@import.row_progress}%; min-width:5%" }

  :javascript
    var current_status = #{ raw json_escape @import.status.to_json };
    var delay = 2000;
    var previousCount = 0;
    function refreshOnStatusChange() {
      $.getJSON('#{administration_import_path(@import, format: :json)}', function(data) {
        if (data.status != current_status) {
          location.reload();
        } else {
          if (previousCount == data.completed_rows_in_stage) {
            delay *= 1.1;
          } else {
            delay = 2000;
          }
          previousCount = data.completed_rows_in_stage;
          setTimeout(refreshOnStatusChange, delay);
          $('#progress-text').html(
            data.completed_rows_in_stage
          ).css(
            'width',
            data.row_progress + '%'
          )
        }
      });
    }
    setTimeout(refreshOnStatusChange, delay);

.row
  .col-lg-4
    .box.box-info
      .box-body
        %table.table.table-no-top-border
          %tr
            %th= t('.table.filename')
            %td= @import.filename
          - if @import.person
            %tr
              %th= t('.table.person')
              %td= link_to @import.person.name, @import.person
          %tr
            %th= t('.table.created_at')
            %td= @import.created_at.to_s(:full)
          %tr
            %th= t('.table.completed_at')
            - if @import.completed_at
              %td= @import.completed_at.to_s(:full)
            - else
              %td
                %em= t('administration.imports.status.pending')
          - if @import.status_at_least?(:parsed)
            %tr
              %th= t('.table.rows')
              %td= @import.rows.count
          %tr
            %th= t('.table.match_strategy')
            %td= t(@import.match_strategy, scope: 'administration.imports.show.match_strategies', default: '') # notest
          %tr
            %th= t('.table.create_as_active')
            %td= t(@import.create_as_active?.to_s, scope: 'administration.imports.show.create_as_active', default: '') # notest

  .col-lg-8
    - if @import.status_at_least?('previewed')
      .box.box-info
        .box-header
          %h3.box-title
            = t('.stats.heading')
            - unless @import.complete?
              (#{t('administration.imports.status.pending')})
        .box-body
          %table.table
            %thead
              %tr
                %th
                %th= t('.table.people')
                %th= t('.table.families')
            %tr
              %td= t('.table.creates')
              %td
                = icon 'fa fa-circle text-green'
                = link_to @import.rows.created_person.count, created_person: true
              %td
                = icon 'fa fa-circle text-green'
                = link_to @import.rows.created_family.count, created_family: true
            %tr
              %td= t('.table.updates')
              %td
                = icon 'fa fa-circle text-yellow'
                = link_to @import.rows.updated_person.count, updated_person: true
              %td
                = icon 'fa fa-circle text-yellow'
                = link_to @import.rows.updated_family.count, updated_family: true
            %tr
              %td= t('.table.no_change')
              %td
                = icon 'fa fa-circle text-gray'
                = link_to @import.rows.unchanged_people.count, unchanged_people: true
              %td
                = icon 'fa fa-circle text-gray'
                = link_to @import.rows.unchanged_families.count, unchanged_families: true
            %tr
              %td= t('.table.errored')
              %td
                = icon 'fa fa-circle text-red'
                = link_to @import.rows.errored.count, errored: true
              %td


- if @import.status_at_least?('previewed')
  = pagination @rows

  %table.table
    %thead
      %tr
        %th= t('.table.person_id')
        %th= t('.table.person_first_name')
        %th= t('.table.person_last_name')
        %th= t('.table.person_matched')
        %th= t('.table.person_status')
        %th= t('.table.family_id')
        %th= t('.table.family_name')
        %th= t('.table.family_matched')
        %th= t('.table.family_status')
    %tbody
      - @rows.each do |row|
        - attrs = row.import_attributes_as_hash(real_attributes: true)
        %tr
          %td= import_link_to(row.person)
          %td= attrs['first_name']
          %td= attrs['last_name']
          %td= t(row.matched_person_by, scope: 'administration.imports.matched_by', default: '') # notest
          %td= import_row_record_status(row, :person)
          %td= import_link_to(row.family)
          %td= attrs['family_name']
          %td= t(row.matched_family_by, scope: 'administration.imports.matched_by', default: '') # notest
          %td= import_row_record_status(row, :family)
        - if row.errored?
          %tr.details
            %td
            %td{ colspan: 8, title: row.import_attributes.inspect }
              %table.spaced-table
                %thead
                  %tr
                    %th= t('.errors_table.name')
                    %th= t('.errors_table.value')
                    %th= t('.errors_table.error')
                %tbody
                  - import_row_errors(row).each do |error|
                    %tr
                      %td= error[:name]
                      %td= error[:value].presence || t('.errors_table.blank_value')
                      %td.text-red= error[:message]

  = pagination @rows

.page-controls
  - if @import.previewed?
    = link_to edit_administration_import_path(@import), class: 'btn btn-warning' do
      = icon 'fa fa-arrow-left'
      = t('.edit.button')
    = link_to execute_administration_import_path(@import), class: 'btn btn-danger', |
      data: { method: 'patch', confirm: t('are_you_sure') } do
      = icon 'fa fa-bolt'
      = t('.execute.button')
  = link_to @import, data: { method: :delete, confirm: t('are_you_sure') }, class: 'btn btn-delete pull-right' do
    = icon 'fa fa-trash-o'
    = t('.delete.button')
  .clearfix
